﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using nova;
using PIMS.DBModel;
using PIMS.WorkPlugin.ORM;

namespace PIMS.WorkPlugin
{
    public partial class Form增加编辑用户 : Form
    {
        private bool isEdit = false;
        private int uid = 0;

        public Form增加编辑用户()
        {
            InitializeComponent();
        }
        public Form增加编辑用户(int id)
        {
            InitializeComponent();
            this.Text = "编辑用户";
            this.uid = id;
            this.isEdit = true;
        }

        private void Form增加用户_Load(object sender, EventArgs e)
        {
            this.班组.Items.Clear();
            this.班组.Items.Add("");
            this.班组.Items.Add("A");
            this.班组.Items.Add("B");
            this.班组.Items.Add("C");
            this.班组.Items.Add("D");
            this.班组.SelectedIndex = 0;

            if (this.isEdit && this.uid > 0)
            {
                try
                {
                    users u = SqlSugarHelper.QueryByKey<users>(this.uid);
                    if (u != null)
                    {
                        用户名.Text = u.UserName;
                        姓名.Text = u.Description;
                        密码.Text = u.Password;
                        密码确认.Text = u.Password;
                        班组.Text = u.Department;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Form增加用户->Form增加用户_Load");
                }
            }
        }

        private void btn_Ack_Click(object sender, EventArgs e)
        {
            try
            {
                this.Err.Clear();
                if (String.IsNullOrEmpty(用户名.Text))
                {
                    this.Err.SetError(this.用户名, "禁止为空！");
                    return;
                }
                if (!isUniqueUserName(用户名.Text))
                {
                    this.Err.SetError(this.用户名, "用户名已使用！");
                    return;
                }
                if (String.IsNullOrEmpty(姓名.Text))
                {
                    this.Err.SetError(this.姓名, "禁止为空！");
                    return;
                }
                if (!isUniqueName(姓名.Text))
                {
                    this.Err.SetError(this.姓名, "姓名已使用！");
                    return;
                }
                if (String.IsNullOrEmpty(密码.Text))
                {
                    this.Err.SetError(this.密码, "禁止为空！");
                    return;
                }
                if (密码.Text != 密码确认.Text)
                {
                    this.Err.SetError(this.密码确认, "两次密码不一到！");
                    return;
                }

                string cmdSql = "";
                if (!this.isEdit)
                {
                    users u = new users();
                    u.UserName = 用户名.Text;
                    u.Password = 密码.Text;
                    u.Description = 姓名.Text;
                    u.Department = 班组.Text;
                    u.Level = "1";

                    if (SqlSugarHelper.InsertRecord<users>(u) > 0)
                    {
                        MessageBox.Show("增加新用户成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        MessageBox.Show("增加新用户失败！", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    users u = SqlSugarHelper.QueryByKey<users>(this.uid);
                    u.UserName = 用户名.Text;
                    u.Password = 密码.Text;
                    u.Description = 姓名.Text;
                    u.Department = 班组.Text;
                    u.Level = "1";

                    if (SqlSugarHelper.UpdateRecord<users>(u) > 0)
                    {
                        MessageBox.Show("编辑用户成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        MessageBox.Show("编辑用户失败！", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Form增加用户->btn_Ack_Click");
            }

        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool isUniqueUserName(string username)
        {
            string cmd = "select * from tb_users where UserName='" + username + "'";
            DataTable dt = SqlSugarHelper.QueryToDataTable<users>(cmd);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (this.isEdit && dt.Rows[0]["ID"].ToString() == this.uid.ToString())
                {
                    return true;
                }
                return false;
            }
            return true;
        }
        private bool isUniqueName(string name)
        {
            string cmd = "select * from tb_users where Description='" + name + "'";
            DataTable dt = SqlSugarHelper.QueryToDataTable<users>(cmd);
            if (dt != null && dt.Rows.Count > 0)
            {
                if (this.isEdit && dt.Rows[0]["ID"].ToString() == this.uid.ToString())
                {
                    return true;
                }
                return false;
            }
            return true;
        }
    }
}
